# Required vars with defaults
# - openqa_createhdds_branch
##       string - The git branch of createhdds to check out
##       default - main

- name: Install required packages
  package:
    name: ['python3-libsemanage', 'libvirt-daemon-kvm', 'libvirt-python3', 'python3-libguestfs',
           'python3-fedfind', 'qemu-kvm', 'virt-install', 'withlock']
    state: present
  tags:
  - packages

- name: Install PowerPC-specific packages
  package: name=powerpc-utils state=present
  when: ansible_architecture == 'ppc64' or ansible_architecture == 'ppc64le'
  tags:
  - packages

- name: Allow libvirt to read/write to NFS
  seboolean: name=virt_use_nfs state=yes persistent=yes

- name: Check if SMT setting needs changing
  command: "ppc64_cpu --smt"
  register: smtcheck
  failed_when: "1 != 1"
  changed_when: "1 != 1"
  check_mode: no
  when: ansible_architecture == 'ppc64' or ansible_architecture == 'ppc64le'

- name: Change SMT setting if necessary
  command: "ppc64_cpu --smt=off"
  when: "smtcheck.stdout is defined and smtcheck.stdout.find('is off') == -1"

- name: Check out createhdds
  git:
    repo: https://pagure.io/fedora-qa/createhdds.git
    dest: /root/createhdds
    version: "{{ openqa_createhdds_branch }}"

- name: Set up createhdds cron job
  copy: src=createhdds dest=/etc/cron.daily/createhdds owner=root group=root mode=0755

- name: Check if any hard disk images need (re)building
  command: "/root/createhdds/createhdds.py check"
  args:
    chdir: /var/lib/openqa/share/factory/hdd/fixed
  register: diskcheck
  failed_when: "1 != 1"
  changed_when: "1 != 1"
  check_mode: no

- name: Ensure libvirt is running if needed to create images
  service: name=libvirtd enabled=yes state=started
  when: "diskcheck.rc > 1"

# > 1 is not a typo; check exits with 1 if all images are present but some
# are outdated, and 2 if any images are missing. We only want to handle
# outright *missing* images here in the playbook (to handle the case of
# first deployment). Outdated images are handled by the daily cron run.
- name: Create hard disk images (this may take a long time!)
  command: "/etc/cron.daily/createhdds"
  when: "diskcheck.rc > 1"
  ignore_errors: yes
